#ifndef ALARMCONTROLLER_H
#define ALARMCONTROLLER_H

#include <QObject>
#include <QDateTime>
#include "models/AlarmModel.h"
#include "models/DataRecordModel.h"
#include "utils/Config.h"
#include "entities/AlarmRecord.h"

/**
 * @brief 报警控制器
 * @details Controller 层，协调报警管理业务流程：
 *          - 报警检测（监听实时数据，检测阈值）
 *          - 报警确认（调用 AlarmModel）
 *          - 阈值配置（调用 Config）
 *          - 报警查询过滤
 *          - 暴露 Q_INVOKABLE 方法给 QML 调用
 */
class AlarmController : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int unacknowledgedCount READ unacknowledgedCount NOTIFY unacknowledgedCountChanged)
    Q_PROPERTY(int criticalAlarmCount READ criticalAlarmCount NOTIFY criticalAlarmCountChanged)
    Q_PROPERTY(int warningAlarmCount READ warningAlarmCount NOTIFY warningAlarmCountChanged)
    Q_PROPERTY(bool alarmDetectionEnabled READ alarmDetectionEnabled WRITE setAlarmDetectionEnabled NOTIFY alarmDetectionEnabledChanged)

public:
    explicit AlarmController(QObject *parent = nullptr);
    ~AlarmController();

    // 设置关联的 Model
    void setAlarmModel(AlarmModel *model);
    void setDataRecordModel(DataRecordModel *model);
    void setConfig(Config *config);

    // 属性访问器
    int unacknowledgedCount() const;
    int criticalAlarmCount() const;
    int warningAlarmCount() const;
    bool alarmDetectionEnabled() const { return m_alarmDetectionEnabled; }
    void setAlarmDetectionEnabled(bool enabled);

    // 报警查询
    Q_INVOKABLE bool loadRecentAlarms(int hours = 24);
    Q_INVOKABLE bool loadUnacknowledgedAlarms();
    Q_INVOKABLE bool loadAlarmsForDevice(const QString &deviceId);
    Q_INVOKABLE bool loadAlarmsByLevel(const QString &level);
    Q_INVOKABLE bool loadAlarmsDateRange(const QDateTime &start, const QDateTime &end);

    // 报警确认
    Q_INVOKABLE bool acknowledgeAlarm(int index, const QString &userName = "admin");
    Q_INVOKABLE bool acknowledgeAlarmById(int alarmId, const QString &userName = "admin");
    Q_INVOKABLE bool acknowledgeAllAlarms(const QString &userName = "admin");

    // 报警管理
    Q_INVOKABLE bool removeAlarm(int index);
    Q_INVOKABLE bool clearAllAlarms();

    // 阈值配置
    Q_INVOKABLE double getTempWarningThreshold() const;
    Q_INVOKABLE double getTempCriticalThreshold() const;
    Q_INVOKABLE double getPressureWarningThreshold() const;
    Q_INVOKABLE double getPressureCriticalThreshold() const;
    Q_INVOKABLE double getFlowWarningThreshold() const;

    Q_INVOKABLE bool setTempWarningThreshold(double value);
    Q_INVOKABLE bool setTempCriticalThreshold(double value);
    Q_INVOKABLE bool setPressureWarningThreshold(double value);
    Q_INVOKABLE bool setPressureCriticalThreshold(double value);
    Q_INVOKABLE bool setFlowWarningThreshold(double value);

    // 手动触发报警检测
    Q_INVOKABLE void checkDataPoint(const QString &deviceId, double temperature, double pressure, double flow);

signals:
    // 属性变化信号
    void unacknowledgedCountChanged();
    void criticalAlarmCountChanged();
    void warningAlarmCountChanged();
    void alarmDetectionEnabledChanged();

    // 新报警信号
    void newAlarm(const QString &deviceId, const QString &alarmType, const QString &level);

    // 报警确认信号
    void alarmAcknowledged(int alarmId);

    // 批量确认完成信号
    void allAlarmsAcknowledged();

    // 阈值变化信号
    void thresholdChanged(const QString &paramName, double newValue);

private slots:
    // 监听实时数据更新
    void onDataUpdated();

    // 监听 AlarmModel 变化
    void onAlarmModelCountChanged();

private:
    // 检测单个数据点是否触发报警
    void detectAlarms(const QString &deviceId, double temperature, double pressure, double flow);

    // 检测温度报警
    void checkTemperatureAlarm(const QString &deviceId, double temperature);

    // 检测压力报警
    void checkPressureAlarm(const QString &deviceId, double pressure);

    // 检测流量报警
    void checkFlowAlarm(const QString &deviceId, double flow);

    // 创建报警记录
    bool createAlarm(const QString &deviceId,
                     const QString &alarmType,
                     const QString &alarmLevel,
                     const QString &paramName,
                     double paramValue,
                     double threshold,
                     const QString &description);

    // 更新统计计数
    void updateAlarmCounts();

private:
    AlarmModel *m_alarmModel;           // 报警模型
    DataRecordModel *m_dataRecordModel; // 实时数据模型
    Config *m_config;                   // 配置管理器

    bool m_alarmDetectionEnabled; // 是否启用报警检测
    int m_unacknowledgedCount;    // 未确认报警数量
    int m_criticalAlarmCount;     // 严重报警数量
    int m_warningAlarmCount;      // 警告报警数量
};

#endif // ALARMCONTROLLER_H
